import time

import requests
from lxml import etree
import pprint

url = "http://www.zhuna.cn/?m=search.getList&page=2&sort=0&distance=3&ecityid=0201&cityname=%E4%B8%8A%E6%B5%B7&hid=&youhui=0&assure=0&lat=&lng=&mapbarname=&mapbarid=0&lsid="

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36',
}


def get_detail_url(url,f):

    response = requests.get(url,headers=headers)
    # 所有信息
    # pprint.pprint(response.json()['data']['hotels'])
    # 获取url
    for data in (response.json()['data']['hotels']):
        base_url = f"http://www.zhuna.cn/hotel-{data.get('id')}.html"
        new_url = base_url
        name = data.get('hotelname')
        price = data.get('Min_Jiage')
        address = data.get('address')

        f.write('url：{}-酒店名称：{}-地址：{}-价格：{}\n'.format(new_url,name,address,price))


def main():
    for i in range(1,100):
        with open('jiudian.csv', 'a', encoding='utf-8') as f:
            url = f"http://www.zhuna.cn/?m=search.getList&page={i}&sort=0&distance=3&ecityid=0201&cityname=%E4%B8%8A%E6%B5%B7&hid=&youhui=0&assure=0&lat=&lng=&mapbarname=&mapbarid=0&lsid="
            get_detail_url(url,f)

if __name__ == '__main__':
    for i in range(12):
        time.sleep(300)
        main()







